//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vParamVersion.h"
//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TvAVersion::TvAVersion(TComponent* Owner)
    : TForm(Owner)
{
    try {
        bbAjouter->Glyph->LoadFromResourceName((int)HInstance, "ID_GBOUTON_NOUVEAU");
        bbDupliquer->Glyph->LoadFromResourceName((int)HInstance, "ID_GBOUTON_DUPLIQUER");
        bbSupprimer->Glyph->LoadFromResourceName((int)HInstance, "ID_GBOUTON_SUPPRIMER");
        bbQuitter->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_QUITTER_24");

        Afficher();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          AFFICHAGE DANS LA LIST VIEW
//---------------------------------------------------------------------------
void __fastcall TvAVersion::Afficher()
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        lvVersion->Items->Clear();
        dmWebHelp = new TdmWebHelp((TComponent*)0);


        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  "select logiciels.log_nom,"    //0
                                        "logiciels.log_code,"  //1
                                        "versions.ver_nom,"    //2
                                        "versions.ver_code "   //3
                                "from versions "
                                    "left outer join logiciels on ( versions.log_code = logiciels.log_code ) "
                                "order by logiciels.log_nom, versions.ver_nom";

        MaRequete->Open();
        MaRequete->First();
        
        TListItem  *ListItem;
        while(!MaRequete->Eof) {
            ListItem = lvVersion->Items->Add();
            //affichage du logiciel avec son code_logiciel
            //--------------------------------------------
            ListItem->SubItems->AddObject(asRequete(0), (TObject*)nRequete(1) );
            //affichage de la version avec son code version
            //---------------------------------------------
            ListItem->SubItems->AddObject(asRequete(2), (TObject*)nRequete(3) );
            //Indiquer le nombre de références
            ListItem->SubItems->Add( CompterReference( nRequete(1),nRequete(3)));

            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}

//---------------------------------------------------------------------------
//          AJOUTER UNE NOUVELLE VERSION
//---------------------------------------------------------------------------
void __fastcall TvAVersion::bbAjouterClick(TObject *Sender)
{
    TAjoutVersion * fNewVersion = NULL;
    try {

        fNewVersion = new TAjoutVersion(Application);
        fNewVersion->ShowModal();
        Afficher();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
     if(fNewVersion) {delete fNewVersion; fNewVersion = NULL;}
}

//---------------------------------------------------------------------------
//          COPIER UNE VERSION
//---------------------------------------------------------------------------
void __fastcall TvAVersion::bbCopierClick(TObject *Sender)
{
    TCopyVersion * fCopyVersion = NULL;
    try {
        TItemStates selected = TItemStates() << isSelected;
        TListItem  *ListItem = lvVersion->Selected;
        if(lvVersion->SelCount != 0) {
            while(ListItem) {
                TCopyVersion * fCopyVersion = NULL;
                fCopyVersion = new TCopyVersion(ListItem->SubItems->Strings[0],(int)ListItem->SubItems->Objects[0],
                                                ListItem->SubItems->Strings[1],(int)ListItem->SubItems->Objects[1],
                                                false,
                                                (TComponent*)0);
                fCopyVersion->ShowModal();
                if(fCopyVersion) {delete fCopyVersion; fCopyVersion = NULL;}
                ListItem = lvVersion->GetNextItem(ListItem,sdAll,selected);
            }
            Afficher();

        }else {
            EICAttention(ERREUR_SELECTION_VERSION);
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(fCopyVersion) {delete fCopyVersion; fCopyVersion = NULL;}
}
//---------------------------------------------------------------------------
//                  SUPPRIMER LES VERSIONS SELECTIONNEES
//---------------------------------------------------------------------------
void __fastcall TvAVersion::bbSupprimerClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try {
        int NombreRef   = 0;

        TItemStates selected = TItemStates() << isSelected;
        TListItem  *ListItem = lvVersion->Selected;
        if(lvVersion->SelCount != 0) {
            NombreRef = CompterReference();
            if(NombreRef == 0) {
                AnsiString asMsg = EmptyStr;
                if(lvVersion->SelCount > 1)
                    asMsg = "Confirmez vous la suppression des versions selectionnées ?";
                else
                    asMsg.sprintf("Confirmez vous la suppression de la version %s associée au logiciel %s ?", ListItem->SubItems->Strings[1],ListItem->SubItems->Strings[0]);

                if (MessageDlg(asMsg, mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes) {

                    Hist = new CHistorique();
                    while(ListItem) {
                        Supprimer((int)ListItem->SubItems->Objects[1]);
                        Hist->AjouterOperation(Histo_SUPPR_VER,
                                                ListItem->SubItems->Strings[0],
                                                ListItem->SubItems->Strings[1]);
                        ListItem = lvVersion->GetNextItem(ListItem,sdAll,selected);
                    }
                    Afficher();

                }
            }else {
                if(lvVersion->SelCount == 1)
                    EICAttention(SUPPRIMER_UNE_VERSION);
                else
                    EICAttention(SUPPRIMER_LES_VERSIONS);
            }
        }else
            EICAttention(ERREUR_SELECTION_VERSION);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}
}
//---------------------------------------------------------------------------
//         COMPTER LE NOMBRE DE REFERENCES (pour la methode Supprimer)
//---------------------------------------------------------------------------
int  __fastcall TvAVersion::CompterReference()
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    int nNombreRerference = 0;
    try {
        TItemStates selected = TItemStates() << isSelected;
        TListItem  *ListItem = lvVersion->Selected;

        dmWebHelp = new TdmWebHelp((TComponent*)0);

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        if(lvVersion->SelCount != 0) {
            while(ListItem) {
                MaRequete->Sql->Text =  (AnsiString)"select count(ref_code) "
                                                    "from refer "
                                                    "where log_code="+(int)ListItem->SubItems->Objects[0]+" and ver_code="+(int)ListItem->SubItems->Objects[1];
                MaRequete->Open();
                MaRequete->First();
                nNombreRerference += nRequete(0);
                MaRequete->Sql->Clear();
                ListItem = lvVersion->GetNextItem(ListItem,sdAll,selected);
            }
        }

        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        nNombreRerference = 0;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    return nNombreRerference;
}
//---------------------------------------------------------------------------
//         COMPTER LE NOMBRE DE REFERENCES (pour la methode Afficher)
//---------------------------------------------------------------------------
int  __fastcall TvAVersion::CompterReference(const int &nLogiciel, const int &nVersion)
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    int nNombreRerference = 0;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select count(ref_code) "
                                            "from refer "
                                            "where log_code="+nLogiciel+" and ver_code="+nVersion;
        MaRequete->Open();
        MaRequete->First();
        nNombreRerference = nRequete(0);
        MaRequete->Sql->Clear();

        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        nNombreRerference = 0;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    return nNombreRerference;
}
//---------------------------------------------------------------------------
//              SUPPRIMER UNE VERSION
//---------------------------------------------------------------------------
void __fastcall TvAVersion::Supprimer(const int &nVersion)
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"delete from versions "+
                                            "where ver_code="+nVersion;
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                          Quitter
//---------------------------------------------------------------------------
void __fastcall TvAVersion::bbQuitterClick(TObject *Sender)
{
    try {
        Close();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------

